package com.mmz.springmvc.addemp;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;
/**
 * 
 * @author Administrator
 *
 */
@SuppressWarnings("deprecation")
public class EmpAction extends AbstractCommandController {
	
	public EmpAction(){
		//将表单参数封装到Emp对象中,然后交给下面处理方法的obj
		this.setCommandClass(Emp.class);
		System.out.println("EmpAction()");
	}


	//自定义类型转换器,将接收到的string类型转换成Date类型(yyyy-MM-dd)
	@Override
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		//向springmvc内部注入一个自定义类型的转换器
		//参数一:将string类型转换成什么类型的字节码
		//参数二:自定义转换规则
		//true表示该日期字段可以为空
		binder.registerCustomEditor(Date.class, 
				new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
	}
	
	/**
	 * obj表示封装后的实体
	 * error表示封装时产生的异常
	 */
	@Override
	protected ModelAndView handle(HttpServletRequest request,
			HttpServletResponse response, 
			Object obj, 
			BindException error)
			throws Exception {
		System.out.println("handle()");
		
		ModelAndView modelAndView = new ModelAndView();
		Emp emp = (Emp) obj;
		System.out.println(emp.getName()+":"+emp.getSex()+":"+emp.getTime());
		
		modelAndView.addObject("msg", emp);
		modelAndView.setViewName("/jsp/success.jsp");
		return modelAndView;
	}

}
